package com.tfswx.jcyw.ruleenginedesigner.api;

import com.tfswx.ruleengine.compute.gzjy.BaseFn;
import com.tfswx.ruleengine.compute.gzjy.RuleEngineUtil;
import com.tfswx.ruleengine.compute.util.ClassUtil;
import com.tfswx.jcyw.ruleenginedesigner.bean.ApiResult;
import com.tfswx.jcyw.ruleenginedesigner.dto.wbffgl.FnListQueryOutputDTO;
import com.tfswx.jcyw.ruleenginedesigner.dto.wbffgl.FnUploadInputDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/7/3
 */
@RestController
@RequestMapping("/wbffgl")
@Api(tags = "外部方法管理")
public class WbffglController {

    @ApiOperation("获取方法文件列表")
    @PostMapping("/queryFnList")
    public ApiResult<List<FnListQueryOutputDTO>> queryFnList() {

        File fnListDir = new File("fnList");
        File[] files = fnListDir.listFiles();
        if (files == null) {
            return ApiResult.success(new ArrayList<>());
        }
        List<FnListQueryOutputDTO> fnList = new ArrayList<>();
        FnListQueryOutputDTO fn;
        for (File file : files) {
            String name = file.getName();
            Long fileCreateTime = getFileCreateTime(file);
            Date date = new Date(fileCreateTime);
            List<BaseFn> fnListByJar = ClassUtil.getFnListByJar(file);

            fn = new FnListQueryOutputDTO();
            fn.setCjsj(date);
            fn.setWjmc(name);
            fn.setFnList(fnListByJar);

            fnList.add(fn);
        }

        return ApiResult.success(fnList);
    }

    @ApiOperation("上传方法包")
    @PostMapping("/uploadFn")
    public ApiResult<Boolean> uploadFn(@Validated FnUploadInputDTO fnUploadInputDTO) throws IOException {
        MultipartFile file = fnUploadInputDTO.getFile();
        String originalFilename = file.getOriginalFilename();
        File fnListDir = new File("fnList").getCanonicalFile();
        if (!fnListDir.exists()) {
            fnListDir.mkdirs();
        }
        File destFile = new File(fnListDir, originalFilename);
        file.transferTo(destFile);
        // 加载类
        ClassUtil.loadClass(destFile);
        RuleEngineUtil.loadFnList();
        return ApiResult.success(true);
    }

    private Long getFileCreateTime(File file) {
        try {
            Path path = Paths.get(file.getPath());
            BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
            BasicFileAttributes attr = basicview.readAttributes();
            return attr.creationTime().toMillis();
        } catch (Exception e) {
            return file.lastModified();
        }
    }
}
